package com.itheima.app.manager.umsManager;

import cn.hutool.core.util.ObjectUtil;
import com.itheima.app.exception.BusinessException;
import com.itheima.commons.PageBean;
import com.itheima.commons.R;
import com.itheima.domain.db.ums.UmsResource;
import com.itheima.service.umsService.UmsResourceService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class UmsResourceManager {

    @DubboReference
    private UmsResourceService umsResourceService;

    //添加后台资源
    public R create(UmsResource umsResource) {
        umsResourceService.create(umsResource);
        return R.ok(null);
    }

    //根据ID删除后台资源
    public R delete(Long id) {
        umsResourceService.delete(id);
        return R.ok(null);
    }

    //分页模糊查询后台资源
    public R getByCondiction(Long categoryId, String nameKeyword, Integer pageNum, Integer pageSize, String urlKeyword) {
        PageBean<UmsResource> res = umsResourceService.getByCondiction(categoryId,nameKeyword,pageNum,pageSize,urlKeyword);
        return R.ok(res);
    }

    //查询所有后台资源
    public R getAll() {
        List<UmsResource> list = umsResourceService.getAll();
        if (ObjectUtil.isEmpty(list)) throw new BusinessException("暂无数据");
        return R.ok(list.toArray());
    }

    //修改后台资源
    public R update(Long id, UmsResource resource) {
        resource.setId(id);
        umsResourceService.update(resource);
        return R.ok(null);
    }

    //根据ID获取资源详情
    public R getById(Long id) {
        UmsResource resource = umsResourceService.getById(id);
        return R.ok(resource);
    }
}
